package com.matianle.springboot_code1.controller;

import com.matianle.springboot_code1.pojo.User;
import com.matianle.springboot_code1.pojo.dto.ReponseMessage;
import com.matianle.springboot_code1.pojo.dto.UserDto;
import com.matianle.springboot_code1.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController   //接口方法返回对象,转换成json脚本
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    //Rest
    //增加
    @PostMapping
    public ReponseMessage<User> add(@Validated @RequestBody UserDto user){
        User userNew = userService.add(user);
        return ReponseMessage.success(userNew);
    }
    //删除
    @DeleteMapping("/{userId}")
    public ReponseMessage<User> delete(@PathVariable Integer userId){
          userService.delete(userId);
        return ReponseMessage.success();
    }
    //修改
    @PutMapping
    public ReponseMessage<User> edit(@Validated @RequestBody UserDto user){
        User userNew = userService.edit(user);
        return ReponseMessage.success(userNew);
    }
    //查询
    @GetMapping("/{userId}")
    public ReponseMessage<User> add(@PathVariable Integer userId){
        User userNew = userService.getUser(userId);
        return ReponseMessage.success(userNew);
    }
}
